﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Text;
using System.Security.Cryptography;

namespace Sendz.Domain
{
    public class User
    {
        // Entity
        [HiddenInput(DisplayValue = false)]
        public Guid UserKey { get; set; }

        [DisplayName("Username")]
        [Required(ErrorMessage = "A username is required")]
        [StringLength(50, ErrorMessage = "Please ensure that the username you enter is less than 50 characters")]
        public string Username { get; set; }

        [DisplayName("Password")]
        [StringLength(50, ErrorMessage = "Please ensure that the password you enter is less than 50 characters")]
        public string Password { get; set; }

        [DisplayName("Is Administrator?")]
        public bool IsAdmin { get; set; }

        [DisplayName("First Name")]
        [Required(ErrorMessage = "A first name is required")]
        [StringLength(50, ErrorMessage = "First names must be less than 50 characters")]
        public string Firstname { get; set; }

        [DisplayName("Last Name")]
        [Required(ErrorMessage = "A last name is required")]
        [StringLength(50, ErrorMessage = "Last names must be less than 50 characters")]
        public string Lastname { get; set; }

        [DisplayName("Email Address")]
        [Required(ErrorMessage = "An email address is required")]
        [RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$", ErrorMessage = "Please enter a valid email address")]
        [StringLength(100, ErrorMessage = "Please ensure that the email from address you enter is less than 100 characters")] 
        public string Email { get; set; }

        [DisplayName("Zipcode")]
        [Required(ErrorMessage = "A zipcode is required")]
        [StringLength(50, ErrorMessage = "Zipcodes must be less than 50 characters")]
        public string Zipcode { get; set; }        

        [DisplayName("Remaining Form/AutoResponder Responses")]
        [DataType(DataType.Text)]
        [Range(0, 99999)]
        public int ResponsesRemaining { get; set; }

        [DisplayName("Remaining Invitations")]
        [DataType(DataType.Text)]
        [Range(0, 100)]
        public int InvitesRemaining { get; set; }

        [HiddenInput(DisplayValue = false)]
        public Guid? InvitedByUserKey { get; set; }

        [HiddenInput(DisplayValue = false)]
        public bool IsRemoved { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime? LastModified { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime? LastLogin { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime CreatedOn { get; set; }

        // Children
        [HiddenInput(DisplayValue = false)]
        public List<Form> Forms { get; set; }
                
        [HiddenInput(DisplayValue = false)]
        public List<AutoResponder> AutoResponders { get; set; }

        [HiddenInput(DisplayValue = false)]
        public List<Identity> Identities { get; set; }

        [HiddenInput(DisplayValue = false)]
        public List<UserEmailValidation> UserEmailValidations { get; set; }

        [HiddenInput(DisplayValue = false)]
        public List<Attachment> Attachments { get; set; }

        // Constructor  
        public User() 
        {
            this.UserKey = Guid.NewGuid();
            this.CreatedOn = DateTime.Now;
        }

        public User(Guid userKey, string email, Guid invitedByUserKey)
        {
            this.UserKey = userKey;
            this.Email = email;
            this.InvitedByUserKey = invitedByUserKey;
            this.CreatedOn = DateTime.Now;
        }
    }
}